<?php
namespace App\Http\Controllers\Api;

use App\Library\OSS;
use App\Modules\Free\Models\FreeRecord;
use App\Modules\Free\Models\UnlockGood;
use App\Modules\Free\Models\UnlockRecord;
use App\Modules\Good\Models\Good;
use App\Modules\Order\Models\Order;
use App\Modules\Package\Models\GoodPackage;
use App\Modules\Rebate\Models\Rebate;
use App\Modules\User\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class FreeController extends ApiController
{
    public function getList(Request $request)
    {
        $this->validate($request, [
            'offset' => 'required|integer',
            'count' => 'required|integer',
        ]);
        $offset = $request->input('offset', 0);
        $count = $request->input('count', 10);
        $user = $this->getUser();

        $unlock = UnlockGood::query()
            ->join('free_records', function ($q) {
                return $q->on('unlock_goods.free_good_id', 'free_records.good_id')
                    ->whereRaw('unlock_goods.free_good_type = free_records.good_type');
            })
            ->where('free_records.user_id', $user->id)
            ->whereRaw('free_records.aim > free_records.unlocked')
            ->selectRaw('unlock_goods.*')
            ->offset($offset)
            ->limit($count)
            ->get();
        if ($unlock->isEmpty()) {
            return $this->success([]);
        }

        $group = $unlock->groupBy('unlock_good_type');
        if (!is_null($group->get('package'))) {
            $package = GoodPackage::query()
                ->whereIn('id', $group->get('package')->pluck('unlock_good_id')->all())
                ->where('status', GoodPackage::STATUS_ON)
                ->get()
                ->keyBy('id');
        }
        if (!is_null($group->get('good'))) {
            $good = Good::query()
                ->whereIn('id', $group->get('good')->pluck('unlock_good_id')->all())
                ->where('status', Good::STATUE_ON)
                ->get()
                ->keyBy('id');
        }

        $re = $unlock->map(function ($item) use($package, $good) {
            if ($item->unlock_good_type == 'package') {
                $tmp = $package->get($item->unlock_good_id);
            } else {
                $tmp = $good->get($item->unlock_good_id);
            }
            return [
                'id' => $tmp->id,
                'good_type' => $item->unlock_good_type,
                'good_id' => $item->unlock_good_id,
                'title' => $tmp->name,
                'cover' => OSS::instance()->getUrl($tmp->order_cover),
                'unlocked' => $item->money,
            ];
        });
        return $this->success($re);
    }

    public function getUnlockGood(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
        ]);
        $id = $request->input('id');
        $user = $this->getUser();

        $freeRecord = FreeRecord::query()
            ->where('id', $id)
            ->where('user_id', $user->id)
            ->first();
        if (is_null($freeRecord)) {
            return $this->fail('id错误');
        }

        $unlock = UnlockGood::query()
            ->where('free_good_type', $freeRecord->good_type)
            ->where('free_good_id', $freeRecord->good_id)
            ->get();
        $group = $unlock->groupBy('free_good_type');

        $package = GoodPackage::query()
            ->whereIn('id', $group->get(Order::TYPE_PACKAGE)->pluck('free_good_id')->all())
            ->get()
            ->keyBy('id');
        $goods = Good::query()
            ->whereIn('id', $group->get(Order::TYPE_GOOD)->pluck('free_good_id')->all())
            ->get()
            ->keyBy('id');
        $re = $unlock->map(function ($item) use($package, $goods) {
            if ($item->type == Order::TYPE_GOOD) {
                $tmp = $goods->get($item->free_good_id);
            } else {
                $tmp = $package->get($item->free_good_id);
            }
            return [
                'id' => $item->free_good_id,
                'type' => $item->free_good_type,
                'name' => $tmp->name,
                'money' => $item->money,
            ];
        });
        return $this->success();
    }

    public function getUnlockList(Request $request)
    {
        $this->validate($request, [
//            'id' => 'required|integer',
            'start' => 'nullable|date',
            'end' => 'nullable|date',
            'offset' => 'nullable|integer',
            'count' => 'nullable|integer',
            'status' => 'nullable|string',
        ]);
//        $id = $request->input('id');
        $start = $request->input('start_time');
        $end = $request->input('end_time');
        $status = $request->input('status');
        $offset = $request->input('offset', 0);
        $count = $request->input('count', 10);
        $user = $this->getUser();

        $query = UnlockRecord::query()
            ->where('user_id', $user->id);
        if (!is_null($status)) {
            $query->where('status', $status);
        }

        if (!is_null($start)) {
            $query->where('created_at', ">", $start);
        }

        if (!is_null($end)) {
            $query->where('created_at', "<", $end);
        }

        $record = $query->orderBy('created_at', 'desc')
            ->offset($offset)
            ->limit($count)
            ->get();
        $user = User::query()->find($record->pluck('buyer_id')->all())->keyBy('id');
        $re = $record->map(function ($item) use($user) {
            return [
                'title' => $item->title,
                'nickname' => $user->get($item->buyer_id)->nickname,
                'avatar' => $user->get($item->buyer_id)->avatar,
                'created_at' => $item->created_at->toDateTimeString(),
                'money' => $item->money,
                'status' => $item->status,
            ];
        });
        return $this->success($re);
    }
}
